//
//  CZMeituanRefreshView.swift
//  刷新控件
//
//  Created by youyoumacmini3 on 2017/7/31.
//  Copyright © 2017年 youyoumacmini3. All rights reserved.
//

import UIKit

class CZMeituanRefreshView: CZRefreshView {


    
    @IBOutlet weak var buildingIconView: UIImageView!
    @IBOutlet weak var earthIconView: UIImageView!
    @IBOutlet weak var petIconView: UIImageView!
    
    
    /// 父视图高度
    override var parentViewHeight: CGFloat{
        didSet{
           // print("父视图高度\(parentViewHeight)")
            
            if parentViewHeight < 20 {
                return
            }
            
            // 20~126  0.2 -> 1
            // 高度差 / 最大高度差
            // 20 = 1 ->0.2   126 = 0 ->1
           
            var scale: CGFloat
            if parentViewHeight > 126 {
                scale = 1
            }else{
                scale = 1 - ((126 - parentViewHeight) / (126 - 20))
            }
    
            petIconView.transform = CGAffineTransform(scaleX: scale, y: scale)
 
        }
    }
    
    
    
    
    override func awakeFromNib() {
        
        // 1. 房子
        let bImage1 = #imageLiteral(resourceName: "icon_building_loading_1")
        let bImage2 = #imageLiteral(resourceName: "icon_building_loading_2")
        
        buildingIconView.image = UIImage.animatedImage(with: [bImage1,bImage2], duration: 0.5)
        
        // 2. 地球
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = -2 * Double.pi
        anim.repeatCount = MAXFLOAT
        anim.duration = 4
        anim.isRemovedOnCompletion = false
        earthIconView.layer.add(anim, forKey: nil)
        
        // 3. 袋鼠
        // 设置袋鼠动画
        let petImage1 = #imageLiteral(resourceName: "icon_small_kangaroo_loading_1")
        let petImage2 = #imageLiteral(resourceName: "icon_small_kangaroo_loading_2")
        
        petIconView.image = UIImage.animatedImage(with: [petImage1,petImage2], duration: 0.5)
        
        // a. 设置锚点
        petIconView.layer.anchorPoint = CGPoint(x: 0.5, y: 1)
        // b. 设置 center
        let x = self.bounds.width * 0.5
        let y = self.bounds.height - 20
        petIconView.center = CGPoint(x: x, y: y)
        
        petIconView.transform = CGAffineTransform(scaleX: 0.4, y: 0.4)
    }

}
